const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}
//封装getSetting
const getSetting=()=>{
  return new Promise((resolve,reject)=>{
    wx.getSetting({
      success:(result)=>{
        resolve(result)
      },
      fail:(err)=>{
        reject(err)
      },
      complete:()=>{}
    })
  })
}
const chooseAddress=()=>{
  return new Promise((resolve,reject)=>{
    wx.chooseAddress({
      success:(result)=>{
        resolve(result)
      },
      fail:(err)=>{
        reject(err)
      },
      complete:()=>{}
    })
  })
}
const openSetting = ()=>{
  return new Promise((resolve,reject)=>{
    wx.openSetting({
      success:(result)=>{
        resolve(result)
      },
      fail:(err)=>{
        reject(err)
      },
      complete:()=>{}
    })
  })
}
const showModal=(title,content)=>{
  return new Promise((resolve,reject)=>{
    wx.showModal({
      title: title,
      content: content,
      success:(result)=>{
        resolve(result)
      },
      fail:(result)=>{
        reject(result)
      }
    })
  })
}
/* 
  promise 形式 showToast
  @params {object} 参数
*/
const showToast=({title})=>{
  return new Promise((resolve,reject)=>{
    wx.showToast({
      title: title,
      icon: 'none',
      duration: 2000,
      success:(res)=>{
        resolve(res)
      },
      fail:(res)=>{
        reject(res)
      }
    })
  })
}
/**
 * promise 形式 login
 */
const login=()=>{
  return new Promise((resolve,reject)=>{
    wx.login({
     timeout:10000,
      success:(res)=>{
        resolve(res)
      },
      fail:(err)=>{
        reject(err)
      }
    })
  })
}
module.exports = {
  formatTime,
  getSetting,
  chooseAddress,
  openSetting,
  showModal,
  showToast,
  login
}
